package com.book.util;

import java.sql.*;

/**
 * dbutil
 *
 * @author liyansheng
 * @date 2023/06/24
 */
public class DBUtil {
    private static final String URL = "jdbc:mysql://localhost:3306/hw_book_sys"; // 替换为你的数据库连接URL
    private static final String USERNAME = "root"; // 替换为你的数据库用户名
    private static final String PASSWORD = "root"; // 替换为你的数据库密码

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver"); // 加载MySQL驱动程序
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USERNAME, PASSWORD);
    }

    public static void closeConnection(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void closeStatement(PreparedStatement stmt) {
        try {
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void closeResultSet(ResultSet rs) {
        try {
            rs.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }



    public static void main(String[] args) {
        try {
            System.out.println(getConnection());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}